// pages/mine/fish/add.js
var app = getApp();
var dateTimePicker = require('../../../utils/dateTimePicker.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    FishFlagHidden1: true,
    FishFlagHidden2: true,
    put_time_Array: null,
    put_time: null,
    begin_time_Array: null,
    begin_time: null,
    second_time_Array: null,
    second_time: null,
    third_time_Array: null,
    third_time: null,
    startYear: 2018,
    endYear: 2028,
    isAddNew: false,
    fieldList: [], //用于钓场列表
    fieldArray: [], //用于Picker
    field_idx: 0,
    fishid: 0,
    btn_submit: false,
    fishinfo: {
      category_id: 55,
      fields: {},
      albums: []

    }
  },
  /**
   * 获取鱼讯详情
   */
  getFishDetail: function(id) {
    wx.showLoading({
      title: '正在加载信息',
    })
    var that = this;
    wx.request({
      url: app.globalData.Domain + '/fish/detail',
      data: {
        id: id
      },
      success: function(res) {
        if (res.data.code == 0) {
          var _detail = res.data.data;
          that.setData({
            fishinfo: _detail
          });

          var _dateTimeArray = that.data.put_time_Array;
          var _put_time = dateTimePicker.getDateArray(new Date(_detail.fields.put_time), _dateTimeArray);

          console.log(_put_time);
          that.setData({
            put_time: _put_time
          });
          if (_detail.goods.length > 0) {

            var _date = new Date(_detail.fields.fishing_time);
            var _time = dateTimePicker.getDateArray(_date, _dateTimeArray);

            console.log(_time);
            that.setData({
              begin_time: _time
            });
          }
          if (_detail.goods.length > 1) {
            var _date = new Date(_detail.fields.second_begin_time);
            var _time = dateTimePicker.getDateArray(_date, _dateTimeArray);
            that.setData({
              second_time: _time,
              FishFlagHidden1: false
            });
          }
          if (_detail.goods.length > 2) {
            var _date = new Date(_detail.fields.third_begin_time);
            var _time = dateTimePicker.getDateArray(_date, _dateTimeArray);
            that.setData({
              third_time: _time,
              FishFlagHidden2: false
            });
          }
        } else {
          wx.showModal({
            title: '哪有鱼提示',
            content: res.data.msg,
            showCancel: false
          })
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },
  /**
   * 获取我的钓场信息，如果没有，则退出
   */
  getMyFields: function(e) {
    var that = this;
    var _token = app.globalData.token;
    wx.request({
      url: app.globalData.Domain + '/field/mylist',
      data: {
        token: _token
      },
      success: function(res) {
        if (res.data.code == 0) {
          var list = res.data.data;

          if (list.length == 0) {
            wx.showModal({
              title: '哪有鱼提示',
              content: "请先登记钓场信息",
              showCancel: false,
              success: function(res) {
                wx.navigateBack({
                  delta: 1,
                })
              }
            })
          } else {
            var arr = [];
            for (var i = 0; i < list.length; i++) {
              arr.push(list[i].title);
            }
            that.setData({
              fieldList: list,
              fieldArray: arr
            });
            var _fishid = that.data.fishid;
            if (_fishid > 0) {
              that.getFishDetail(_fishid);
            } else {
              that.getLastedFish();
            }
          }
        } else {
          wx.showModal({
            title: '哪有鱼提示',
            content: res.data.msg,
            showCancel: false,
            success: function(res) {
              wx.navigateBack({
                delta: 1,
              })
            }
          })

        }
      }
    })
  },
  getLastedFish: function(e) {
    var that = this;
    var _token = app.globalData.token;
    wx.showLoading({
      title: '加载信息模板...',
    })
    wx.request({
      url: app.globalData.Domain + '/fish/mylasted',
      data: {
        token: _token
      },
      success: function(res) {
        if (res.data.code == 0) {
          var obj = res.data.data;
          obj.id = 0;
          obj.albums = [];
          obj.img_url = "";
          obj.fields.fish_status = null;
          if (obj.goods.length > 1) {
            that.setData({
              FishFlagHidden1: false
            });
          }
          if (obj.goods.length > 2) {
            that.setData({
              FishFlagHidden2: false
            });
          }
          if (obj) {
            that.setData({
              fishinfo: obj
            });
          }
        }
      },
      complete: () => {
        wx.hideLoading();
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (!app.globalData.userInfo) {
      wx.showModal({
        title: '哪有鱼提示',
        content: '没有登录信息哦',
        showCancel: false,
        success: function(res) {
          wx.navigateBack();
          return;
        }
      })
    }
    var that = this;
    var _put_time = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    _put_time.dateTimeArray.pop();
    _put_time.dateTime[4] = 0; //设置时为整点
    _put_time.dateTime.pop();

    var _begin_time = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    _begin_time.dateTimeArray.pop();
    _begin_time.dateTime.pop();
    _begin_time.dateTime[4] = 0; //设置时为整点
    var _second_time = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    _second_time.dateTimeArray.pop();
    _second_time.dateTime.pop();
    _second_time.dateTime[4] = 0; //设置时为整点
    var _third_time = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    _third_time.dateTimeArray.pop();
    _third_time.dateTime.pop();
    _third_time.dateTime[4] = 0; //设置时为整点
    that.setData({
      put_time_Array: _put_time.dateTimeArray,
      put_time: _put_time.dateTime,
      begin_time_Array: _begin_time.dateTimeArray,
      begin_time: _begin_time.dateTime,
      second_time_Array: _second_time.dateTimeArray,
      second_time: _second_time.dateTime,
      third_time_Array: _third_time.dateTimeArray,
      third_time: _third_time.dateTime
    });
    if (options.id && options.id > 0) {
      that.setData({
        fishid: options.id
      })
    }
    that.getMyFields();
  },

  /**
   * 钓场选择改变
   */
  bindFieldChange: function(e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      field_idx: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 遛鱼选项
   */
  lyCKChange: function(e) {
    console.log(e.detail.value);
    var that = this;
    var flag1 = true;
    var flag2 = true;
    var sels = e.detail.value
    for (var i = 0; i < sels.length; i++) {
      if (sels[i] == "cb1") {
        flag1 = false;
      }
      if (sels[i] == "cb2") {
        flag2 = false
      }
    }
    that.setData({
      "FishFlagHidden1": flag1,
      "FishFlagHidden2": flag2
    })
  },
  /**
   * 时间picker
   */
  changeTime(e) {
    console.log(e);
    if (e.currentTarget.id == "picker_put_time") {
      this.setData({
        put_time: e.detail.value
      });
    } else if (e.currentTarget.id == "picker_begin_time") {
      this.setData({
        begin_time: e.detail.value
      });
    } else if (e.currentTarget.id == "picker_second_time") {
      this.setData({
        second_time: e.detail.value
      });
    }

  },
  changeTimeColumn(e) {

    if (e.currentTarget.id == "picker_put_time") {
      var arr = this.data.put_time;
      var dateArr = this.data.put_time_Array;
      arr[e.detail.column] = e.detail.value;

      dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
      this.setData({
        put_time_Array: dateArr,
        put_time: arr
      });
    } else if (e.currentTarget.id == "picker_begin_time") {
      var arr = this.data.begin_time;
      var dateArr = this.data.begin_time_Array;
      arr[e.detail.column] = e.detail.value;
      dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
      this.setData({
        begin_time_Array: dateArr,
        begin_time: arr
      });
    } else if (e.currentTarget.id == "picker_second_time") {
      var arr = this.data.second_time;
      var dateArr = this.data.second_time_Array;
      arr[e.detail.column] = e.detail.value;
      dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
      this.setData({
        second_time_Array: dateArr,
        second_time: arr
      });
    } else if (e.currentTarget.id == "picker_third_time") {
      var arr = this.data.third_time;
      var dateArr = this.data.third_time_Array;
      arr[e.detail.column] = e.detail.value;
      dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
      this.setData({
        third_time_Array: dateArr,
        third_time: arr
      });
    }
  },
  formSubmit: function(e) {
    var that = this;
    var _token = app.globalData.token;
    var _model = that.data.fishinfo;
    //debugger;
    //console.log(e.detail.value);
    wx.showLoading({
      title: '正在保存...',
    })
    that.setData({
      btn_submit: true
    });
    //_model.fields.user_id=
    _model.content = e.detail.value.content;
    _model.fields.fish_weight = e.detail.value.fish_weight;
    _model.fields.fish_desc = e.detail.value.fish_desc;
    let _put_time = that.data.put_time;
    let _put_time_array = that.data.put_time_Array;
    var field_sel_idx = e.detail.value.field_idx;
    _model.fields.field_title = that.data.fieldList[field_sel_idx].title;
    _model.fields.field_id = that.data.fieldList[field_sel_idx].id;
    _model.fields.put_time = _put_time_array[0][_put_time[0]] + "-" + _put_time_array[1][_put_time[1]] + "-" + _put_time_array[2][_put_time[2]] + " " + _put_time_array[3][_put_time[3]] + ":" + _put_time_array[4][_put_time[4]] + ":00";

    let _begin_time = that.data.begin_time;
    let _begin_time_array = that.data.begin_time_Array;

    _model.fields.fishing_time = _begin_time_array[0][_begin_time[0]] + "-" + _begin_time_array[1][_begin_time[1]] + "-" + _begin_time_array[2][_begin_time[2]] + " " + _begin_time_array[3][_begin_time[3]] + ":" + _begin_time_array[4][_begin_time[4]] + ":00";
    _model.specs = [{
      channel_id: 8,
      spec_id: 23,
      parent_id: 0,
      title: "进场",
      img_url: ""
    }, {
      channel_id: 8,
      parent_id: 23,
      spec_id: 24,
      parent_id: 0,
      title: "主场",
      img_url: ""
    }];
    _model.goods = [{
      channel_id: 8,
      market_price: e.detail.value.frist_market_price,
      sell_price: e.detail.value.frist_sell_price,
      stock_quantity: e.detail.value.first_quantity,
      spec_ids: "24",
      spec_text: "场次：主场"
    }];
    if (!that.data.FishFlagHidden1) {

      let _second_time = that.data.second_time;
      let _second_time_array = that.data.second_time_Array;

      _model.fields.second_begin_time = _second_time_array[0][_second_time[0]] + "-" + _second_time_array[1][_second_time[1]] + "-" + _second_time_array[2][_second_time[2]] + " " + _second_time_array[3][_second_time[3]] + ":" + _second_time_array[4][_second_time[4]] + ":00";
      _model.specs.push({
        channel_id: 8,
        parent_id: 23,
        spec_id: 25,
        parent_id: 0,
        title: "遛鱼场1",
        img_url: ""
      });
      _model.goods.push({
        channel_id: 8,
        market_price: e.detail.value.second_market_price,
        sell_price: e.detail.value.second_sell_price,
        stock_quantity: e.detail.value.first_quantity,
        pec_ids: "25",
        spec_text: "场次：遛鱼场1"
      });
    }
    if (!that.data.FishFlagHidden2) {
      let _third_time = that.data.third_time;
      let _third_time_array = that.data.third_time_Array;
      _model.fields.third_begin_time = _third_time_array[0][_third_time[0]] + "-" + _third_time_array[1][_third_time[1]] + "-" + _third_time_array[2][_third_time[2]] + " " + _third_time_array[3][_third_time[3]] + ":" + _third_time_array[4][_third_time[4]] + ":00";
      _model.specs.push({
        channel_id: 8,
        parent_id: 23,
        spec_id: 26,
        parent_id: 0,
        title: "遛鱼场2",
        img_url: ""
      });
      _model.goods.push({
        channel_id: 8,
        market_price: e.detail.value.third_market_price,
        sell_price: e.detail.value.third_sell_price,
        stock_quantity: e.detail.value.first_quantity,
        pec_ids: "26",
        spec_text: "场次：遛鱼场2"
      });
    }
    _model.fields.first_period = e.detail.value.first_period;

    _model.fields.second_period = e.detail.value.second_period;

    _model.fields.third_period = e.detail.value.third_period;
    _model.fields.limit_val = e.detail.value.limit_val;
    _model.fields.fish_bait_desc = e.detail.value.fish_bait;
    _model.fields.buy_back_desc = e.detail.value.buy_back_desc;
    _model.fields.mark_flag_desc = e.detail.value.mark_flag_desc;

    wx.request({
      method: 'POST',
      url: app.globalData.Domain + '/fish/save', //接口地址
      data: {
        token: _token,
        model: _model
      },
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        if (res.data.code == 0) {
          wx.showToast({
            title: '信息发布成功',
            duration: 2000
          })
          setTimeout(function() {
            wx.navigateBack();
          }, 2000)
        } else {
          wx.showModal({
            title: '哪有鱼提示',
            content: res.data.msg
          })
        }
      },
      fail: function(res) {
        wx.showModal({
          title: '哪有鱼提示',
          content: res
        })
      },
      complete: (res) => {
        wx.hideLoading();
        that.setData({
          btn_submit: false
        });
      }
    })
  },
  toPutFish: function(e) {
    var _id = e.currentTarget.dataset.id;
    var _title = e.currentTarget.dataset.title;
    var _field_title = e.currentTarget.dataset.field_title;
    wx.navigateTo({
      url: "putfish?id=" + _id + "&title=" + _title + "&f_title=" + _field_title
    })
  },
})